﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AISservice.Common
{
    public class Pair<TFirst, TSecond> : IComparable
    {
        private TFirst _first;

        public TFirst First
        {
            get { return _first; }
            set { _first = value; }
        }
        private TSecond _second;

        public TSecond Second
        {
            get { return _second; }
            set { _second = value; }
        }

        private bool _BothKey;

        private string _DisplayText;

        public Pair()
        {
            _BothKey = false;
        }
        public Pair(TFirst k, TSecond v)
        {
            First = k;
            Second = v;
            _BothKey = false;
        }
        public Pair(TFirst k, TSecond v, string display)
        {
            First = k;
            Second = v;
            _DisplayText = display;
            _BothKey = false;
        }
        public Pair(TFirst k, TSecond v, bool bothKey)
        {
            First = k;
            Second = v;
            _BothKey = bothKey;
        }
        public Pair(TFirst k, TSecond v, string display, bool bothKey)
        {
            First = k;
            Second = v;
            _DisplayText = display;
            _BothKey = bothKey;
        }

        public override string ToString()
        {
            return string.IsNullOrEmpty(_DisplayText) ? string.Format("{0}:{1}", First, Second) : _DisplayText;
        }

        public override bool Equals(object obj)
        {
            if (_BothKey)
            {
                Pair<TFirst, TSecond> item = obj as Pair<TFirst, TSecond>;
                if (item == null)
                    return false;

                return this.First.Equals(item.First) && this.Second.Equals(item.Second);
            }
            else
            {
                if (obj is TFirst)
                    return First.Equals(obj);

                Pair<TFirst, TSecond> item = obj as Pair<TFirst, TSecond>;
                if (item == null)
                    return false;

                return this.First.Equals(item.First);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public int CompareTo(Object obj)
        {
            int error_result = 0;
            if (_BothKey)
            {
                if (obj is Pair<TFirst, TSecond> && First is IComparable && Second is IComparable)
                {
                    int res = (First as IComparable).CompareTo((obj as Pair<TFirst, TSecond>).First);
                    if (res != 0)
                        return res;
                    return (Second as IComparable).CompareTo((obj as Pair<TFirst, TSecond>).Second);
                }
            }
            else
            {
                if (obj is TFirst)
                {
                    if (First is IComparable)
                    {
                        return (First as IComparable).CompareTo(obj);
                    }
                    else
                        return error_result;
                }
                else if (obj is Pair<TFirst, TSecond>)
                {
                    if (First is IComparable)
                    {
                        return (First as IComparable).CompareTo((obj as Pair<TFirst, TSecond>).First);
                    }
                }
            }
            return error_result;
        }
    }
}
